package com.yyys.web.service.impl;

import com.yyys.web.json.DRDailyRecommendJson;
import com.yyys.web.json.DRGoods;
import com.yyys.web.json.DRGoodsJson;
import com.yyys.web.repository.DRGoodsRepository;
import com.yyys.web.service.DRGoodsInfoService;
import com.yyys.web.utils.Constant;
import com.yyys.web.utils.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengsenwen on 2017/8/12.
 */

@Service
public class DRGoodsInfoServiceImpl implements DRGoodsInfoService {
    Logger logger = Logger.getLogger(DRGoodsInfoServiceImpl.class);

    @Autowired
    private DRGoodsRepository drGoodsRepository;

    @Override
    public List<DRGoods> getByDateDRClassAndCBName(Integer drId) throws Exception {

        List<Object[]> objects = new ArrayList<>();
        List<DRGoods> drGoodsList = new ArrayList<>();

        String rq = DateUtils.getCurrentDay();
        String cbName = DateUtils.getBreakfastLunchOrDinner();
        List<String> stringList = drGoodsRepository.findSpClaseNameByDrId(drId, cbName, rq);

        for (String str : stringList) {
            DRGoods drGoods = new DRGoods();
            drGoods.setTypeName(str);

            for (int i = 0; i < stringList.size(); i++) {
                objects = drGoodsRepository.findByDateDRClassAndCBName(rq, drId, str, cbName);

                // TODO: 2017/8/14  在for循环中只执行一次，所以把这段代码提出来放在方法：objectsToDRGoodsJson
                drGoods.setDrGoodsJsonList(objectsToDRGoodsJson(objects));
            }

            drGoodsList.add(drGoods);
        }

        return drGoodsList;
    }

    private List<DRGoodsJson> objectsToDRGoodsJson(List<Object[]> objects) {
        List<DRGoodsJson> drGoodsJsonList = new ArrayList<>();
        // TODO: 2017/8/14 将返回结果转换为DRGoodsJson
        for (Object[] o : objects) {
            DRGoodsJson drGoodsJson = new DRGoodsJson();

//            @Query(value = "SELECT d.drId,d.rq,d.spId,d.spName,d.spClassName,d.spPrice,d.spPic,d.spFlavor,d.sprIndex," +
//                    "d.cbId,d.mrtj,c.id,c.cbbh,c.cbName,c.bngTime,c.endTime FROM DRGoodsInfo d, CBInfo c WHERE d.cbId = c.id  " +
//                    "AND d.rq=?1  AND d.drId = ?2 AND d.spClassName = ?3")

            drGoodsJson.setDrId(Integer.valueOf(String.valueOf(o[0])));
            drGoodsJson.setRq(String.valueOf(o[1]));
            drGoodsJson.setSpId(String.valueOf(o[2]));
            drGoodsJson.setSpName(String.valueOf(o[3]));
            drGoodsJson.setSpClassName(String.valueOf(o[4]));
            drGoodsJson.setSpPrice((BigDecimal) o[5]);
            drGoodsJson.setSpPic(String.valueOf(o[6]));
            drGoodsJson.setSpFlavor(String.valueOf(o[7]));
            drGoodsJson.setSprIndex(Integer.valueOf(String.valueOf(o[8])));
            drGoodsJson.setCbId(Integer.valueOf(String.valueOf(o[9])));
            drGoodsJson.setMrtj(String.valueOf(o[10]));
            drGoodsJson.setId(Integer.valueOf(String.valueOf(o[11])));
            drGoodsJson.setCbbh(String.valueOf(o[12]));
            drGoodsJson.setCbName(String.valueOf(o[13]));
            drGoodsJson.setBngTime(String.valueOf(o[14]));
            drGoodsJson.setEndTime(String.valueOf(o[15]));

            drGoodsJsonList.add(drGoodsJson);
        }

        return drGoodsJsonList;
    }


    @Override
    public List<DRDailyRecommendJson> getDailyRecommend(Integer drId) throws Exception {
        List<Object[]> objects = new ArrayList<>();
        List<DRDailyRecommendJson> drDailyRecommendJsonList = new ArrayList<>();

        String rq = DateUtils.getCurrentDay();
        objects = drGoodsRepository.findDailyRecommendByRQRAndDRID(rq, drId, Constant.DAILY_RECOMMEND_TAG);
        if (objects != null) {
            drDailyRecommendJsonList = objectsToDRDailyRecommendJson(objects);
        } else {
            logger.info("getDailyRecommend:dailyRecommend dish is null");
        }
        return drDailyRecommendJsonList;
    }

    private List<DRDailyRecommendJson> objectsToDRDailyRecommendJson(List<Object[]> objects) {
        List<DRDailyRecommendJson> drDailyRecommendJsonList = new ArrayList<>();
        // TODO: 2017/8/14 将返回结果转换为DRGoodsJson
        for (Object[] o : objects) {
            DRDailyRecommendJson drDailyRecommendJson = new DRDailyRecommendJson();

//            SELECT d.drId,d.rq,d.spId,d.spName,d.spClassName,d.spPrice,d.spPic,d.spFlavor," +
//            "d.sprIndex,d.cbId,d.mrtj FROM DRGoodsInfo d

            drDailyRecommendJson.setDrId(Integer.valueOf(String.valueOf(o[0])));
            drDailyRecommendJson.setRq(String.valueOf(o[1]));
            drDailyRecommendJson.setSpId(String.valueOf(o[2]));
            drDailyRecommendJson.setSpName(String.valueOf(o[3]));
            drDailyRecommendJson.setSpClassName(String.valueOf(o[4]));
            drDailyRecommendJson.setSpPrice((BigDecimal) o[5]);
            drDailyRecommendJson.setSpPic(String.valueOf(o[6]));
            drDailyRecommendJson.setSpFlavor(String.valueOf(o[7]));
            drDailyRecommendJson.setSprIndex(Integer.valueOf(String.valueOf(o[8])));
            drDailyRecommendJson.setCbId(Integer.valueOf(String.valueOf(o[9])));
            drDailyRecommendJson.setMrtj(String.valueOf(o[10]));

            drDailyRecommendJsonList.add(drDailyRecommendJson);
        }

        return drDailyRecommendJsonList;
    }


    @Override
    public List<DRGoodsJson> getRecommendGoodBydrIdAndspId(Integer drId, String spId) throws Exception {
        List<DRGoodsJson> drGoodsJsonList = new ArrayList<>();
        List<Object[]> objects = new ArrayList<>();

        String rq = DateUtils.getCurrentDay();

        objects = drGoodsRepository.findGoodByDrIdSpIdAndCbId(rq, drId, spId);
        drGoodsJsonList = objectsToDRGoodsJson(objects);

        return drGoodsJsonList;
    }

}
